--################# HEADER #################
AddCSLuaFile("cl_init.lua");
AddCSLuaFile("shared.lua");
include("shared.lua");
--################# SENT CODE ###############

--################# Init @JDM12989
function ENT:Initialize()
	self.Entity:SetModel("models/sandeno/naquadah_bottle.mdl");
	self.Entity:PhysicsInit(SOLID_VPHYSICS);
	self.Entity:SetMoveType(MOVETYPE_VPHYSICS);
	self.Entity:SetSolid(SOLID_VPHYSICS);
					 
	self.MaxEnergy = StarGate.CFG:Get("Naquadah_Bottle","MaxEnergy",30000);    --@meeces2911 WHAT is this energy ment to be, it was set to 500!? that it not very much energy ... ? Did you want 500 NE ?
	self.MaxStorage = StarGate.CFG:Get("Naquadah_Bottle","MaxStorage",800);
	self.enabled = false;
	self:AddResource("NE",self.MaxEnergy); --Naquadah energy @Anorr
	self:SupplyResource("NE",self.MaxEnergy);
	self:AddResource("energy",self.MaxStorage); -- Maximum energy to store in a Naquadah Bottle is 800 units
	self:CreateWireOutputs("Active","Energy","Naquadah %","Naquadah Energy");
	self:CreateWireInputs("Disable");

local phys = self.Entity:GetPhysicsObject();
	if(phys:IsValid()) then
		phys:Wake();
		phys:SetMass(5);
	end
end

--################# Spawn the SENT @JDM12989
function ENT:SpawnFunction(p,t)
	if(not t.Hit) then return end;
	local e = ents.Create("naquadah_bottle");
	e:SetPos(t.HitPos+Vector(0,0,10));
	e:Spawn();
	return e;
end

--################# Adds the annoying overlay speechbubble to this SENT @JDM12989
function ENT:ShowOutput(v,force)
	local add = "(Off)";
	if(self.enabled) then add = "(Active)" end;
	self:SetOverlayText("Naquadah Bottle "..add.."\n"..v.."%");
end

--################# Think @JDM12989
function ENT:Think()
	if(self.depleted or not self.HasResourceDistribution) then return end;
		local energy = self:GetResource("energy");
		local NE = self:GetResource("NE");
		local my_capacity = self:GetUnitCapacity("energy");
		local nw_capacity = self:GetNetworkCapacity("energy");
		local percent = (NE/self.MaxEnergy)*100;
		if(not self.disabled) then
			self.resfix = false;
			if(my_capacity ~= nw_capacity) then -- We are connected to a network - Enable Naquadah
				if(not self.enabled) then
					self.Entity:SetMaterial("models/sandeno/liquid.vmt");
					self.enabled = true;
				end
			else
				if(self.enabled) then
					self.Entity:SetMaterial("models/sandeno/base.vmt");
					self.enabled = false;
				end
			end
		else
		self.enabled = false;
		if(not self.resfix) then
			self:ConsumeResource("energy",800);   -- This is to remove the excess energy stored in an energy cell, if ZPM is disabled.
			self:SupplyResource("NE",800);  -- This is adding the removed energy back into the ZPM
		end
		self.resfix = true;
	end
	-- No Naquadah Energy available anymore - We are depleted!
	if(NE == 0) then
		self.Entity:SetMaterial("Zup/ZPM/zpm_depleted.vmt");
		self.depleted = true;
		self.enabled = false;
		self:SetOverlayText("Naquada Bottle\nDepleted");
	end
	-- Energy conversion when availeble storage @Anorr,aVoN
	if(self.enabled and not self.disabled and energy < nw_capacity) then
		local rate = (my_capacity+nw_capacity)/2; -- Two passes until it filled the full network
		rate = math.Clamp(rate,0,NE);
		rate = math.Clamp(rate,0,nw_capacity-energy);
		self:SupplyResource("energy",rate);
		self:ConsumeResource("NE",rate);
	end
	if(self.depleted and Dev_Unlink_All) then
		Dev_Unlink_All(self.Entity);
		self:SetWire("Active",-1);
	else
		self:ShowOutput(percent);
		self:SetWire("Active",self.enabled);
		self:SetWire("Energy",math.floor(energy));
		self:SetWire("Naquadah Energy",math.floor(NE));
		self:SetWire("Naquadah %",percent);
	end
	self.Entity:NextThink(CurTime()+0.5);
	return true;
end

--Using aVoN's Wire class thingy ??
function ENT:TriggerInput(name,value)
	if(name == "Disable") then
		if (value == 1) then
			self:SetWire("Disabled",1);
			self.disabled = true;
		else
			self:SetWire("Disabled",0);
			self.disabled = false;
		end
	end
end